/**
 * Copyright (c) 2006-2007 Jinyi.lu
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of GNU LGPL license by gun.org
 * which accompanies this distribution, and is available at
 * http://gnu.feld-it.at/copyleft/lesser.html
 * 
 * Project Homepage : http://code.google.com/p/demou
 * Source Code: svn checkout http://demou.googlecode.com/svn/trunk/ demou
 * Mailing list : jinyi.lu@gmail.com
 *                stonelu@126.com
 *
 *  
 * Contributors:
 *     JinYi.Lu - initial API and implementation
 */
package cn.tworen.demou.handler.invoke;

import net.sf.cindy.Buffer;
import net.sf.cindy.buffer.BufferFactory;

import org.apache.log4j.Logger;

import cn.tworen.demou.amf.AMF0;
import cn.tworen.demou.cmd.CmdName;
import cn.tworen.demou.cmd.ICmd;
import cn.tworen.demou.cmd.IInvokeCmd;
import cn.tworen.demou.cmd.InvokeCmd;
import cn.tworen.demou.io.AsBean;
import cn.tworen.demou.rtmp.Client;
import cn.tworen.demou.rtmp.NCInfo;

/**
 * Created on  2007-1-20
 *
 * Title       : CreateStream.java
 * Description : 
 * 
 * @author     : LuJinYi
 * @version    : 1.0
 * @Date       : 2007-1-20
 * History     : 
 * 
 */
public class CreateStream implements IInvoke{
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CreateStream.class);

	public void invoke(Client client, IInvokeCmd invoekCmd) {
		logger.debug("CreateStream");
		
		IInvokeCmd cmdout = new InvokeCmd(client.getInvokeDataType());
		cmdout.setHeader(invoekCmd.getHeader().clone());
		cmdout.getHeader().setChunkIndex(200);
		cmdout.setTrxId(invoekCmd.getTrxId());
		cmdout.setCmdName(CmdName._result);
		cmdout.getHeader().setHeaderLenght(12);
//		int now = (int) (System.currentTimeMillis() & 0xffffffff);
//		Buffer buffer = BufferFactory.allocate(9);
//		buffer.put(AMF0.NumberType);
//		buffer.putDouble(client.getStreamSize() + 1);
//		buffer.flip();
////		cmdout.setParam(buffer);
		cmdout.addParam(client.getStreamSize() + 1);
		
		
		AsBean<String, Object> info = new AsBean<String, Object>();
		info.put(NCInfo.INFO_LEVEL, NCInfo.INFO_VAULE_STATUS);
		info.put(NCInfo.INFO_CODE, NCInfo.NC_CONNECTION_SUCCESS);
		info.put(NCInfo.INFO_DESCRIPTION, "Connection succeeded.");
		info.put(NCInfo.INFO_DETAILS, null);
		info.put(NCInfo.INFO_OBJECT_ENCODING, client.getObjectEncoding());
		
		IInvokeCmd successCmd = new InvokeCmd(client.getInvokeDataType());
		successCmd.getHeader().setChunkIndex(195);
		successCmd.setTrxId(invoekCmd.getTrxId());
		successCmd.setCmdName(CmdName._result);
		successCmd.getHeader().setDataType(cmdout.getHeader().getDataType());
		successCmd.getHeader().setHeaderLenght(12);
		successCmd.getHeader().setSource(cmdout.getHeader().getSource());//source NetConnection
		successCmd.addParam(info);
		
		client.getRoutePacketOut().route(new ICmd[]{cmdout, successCmd});
	}
	private void createFaild() {
		AsBean<String, Object> info = new AsBean<String, Object>();
		info.put(NCInfo.INFO_LEVEL, NCInfo.INFO_VAULE_STATUS);
		info.put(NCInfo.INFO_CODE, NCInfo.NC_CONNECTION_SUCCESS);
		info.put(NCInfo.INFO_DESCRIPTION, "Connection succeeded.");
		info.put(NCInfo.INFO_DETAILS, null);
//		info.put(InfoObject.INFO_OBJECT_ENCODING, client.getObjectEncoding());
	}

}
